package com.rickpan.controller;

import com.rickpan.dto.common.ApiResponse;
import com.rickpan.utils.SecurityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器
 */
@RestController
@RequestMapping("/api/test")
public class TestController {

    /**
     * 公开接口测试
     */
    @GetMapping("/public")
    public ApiResponse<Map<String, Object>> publicTest() {
        Map<String, Object> data = new HashMap<>();
        data.put("message", "这是一个公开接口");
        data.put("timestamp", LocalDateTime.now());
        data.put("authenticated", SecurityUtils.isAuthenticated());
        
        return ApiResponse.success("公开接口调用成功", data);
    }

    /**
     * 需要认证的接口测试
     */
    @GetMapping("/private")
    public ApiResponse<Map<String, Object>> privateTest() {
        Map<String, Object> data = new HashMap<>();
        data.put("message", "这是一个需要认证的接口");
        data.put("timestamp", LocalDateTime.now());
        data.put("userId", SecurityUtils.getCurrentUserId());
        data.put("username", SecurityUtils.getCurrentUsername());
        data.put("isAdmin", SecurityUtils.isAdmin());
        
        return ApiResponse.success("认证接口调用成功", data);
    }

    /**
     * 管理员接口测试
     */
    @GetMapping("/admin")
    public ApiResponse<Map<String, Object>> adminTest() {
        if (!SecurityUtils.isAdmin()) {
            return ApiResponse.forbidden("需要管理员权限");
        }
        
        Map<String, Object> data = new HashMap<>();
        data.put("message", "这是一个管理员接口");
        data.put("timestamp", LocalDateTime.now());
        data.put("userId", SecurityUtils.getCurrentUserId());
        data.put("username", SecurityUtils.getCurrentUsername());
        
        return ApiResponse.success("管理员接口调用成功", data);
    }
}
